package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacCulBottle;
import com.ysd.lis.entity.lab.LabRptGroup;
import com.ysd.lis.mapper.bac.BacCulBottleMapper;
import com.ysd.lis.service.bac.BacCulBottleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 培养瓶培养架 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-08-21
 */
@Service
public class BacCulBottleServiceImpl extends ServiceImpl<BacCulBottleMapper, BacCulBottle> implements BacCulBottleService {

    @Autowired
    BacCulBottleMapper bacCulBottleMapper;

    @Override
    public Result findBacCulBottleList(BacCulBottle bacCulBottle) {
        List<BacCulBottle> list = findBacCulBottleArray(bacCulBottle);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacCulBottle> findBacCulBottleArray(BacCulBottle bacCulBottle) {
        MPJLambdaWrapper<BacCulBottle> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacCulBottle.class);
        queryWrapper.selectAs("rptgrp", LabRptGroup::getRptGroupName, BacCulBottle::getRptGroupNa);
        queryWrapper.eq(BacCulBottle::getDelFlag, 0);
        queryWrapper.leftJoin(LabRptGroup.class, "rptgrp", p -> p.eq(LabRptGroup::getRptGroup, BacCulBottle::getRptGroup).eq(LabRptGroup::getDelFlag, 0));
        queryWrapper.eq(StringUtils.isNotBlank(bacCulBottle.getId()), BacCulBottle::getId, bacCulBottle.getId());
        return bacCulBottleMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacCulBottle(BacCulBottle bacCulBottle) {
        int i = bacCulBottleMapper.insert(bacCulBottle);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacCulBottle);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacCulBottle(BacCulBottle bacCulBottle) {
        bacCulBottle.setDelFlag(1);
        int i = bacCulBottleMapper.updateById(bacCulBottle);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacCulBottle);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacCulBottle(BacCulBottle bacCulBottle) {
        int i = bacCulBottleMapper.updateById(bacCulBottle);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacCulBottle);
        }
        return Result.fail("更新失败");
    }

    @Override
    public BacCulBottle findOneByRptGroup(String rptGroup) {
        MPJLambdaWrapper<BacCulBottle> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacCulBottle::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(rptGroup), BacCulBottle::getRptGroup, rptGroup);
        queryWrapper.orderByAsc(BacCulBottle::getSeq);
        List<BacCulBottle> list = bacCulBottleMapper.selectList(queryWrapper);

        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }
}
